﻿<script type="text/javascript">

_spBodyOnLoadFunctionNames.push("addEvents");


function addEvents() {

//get list name control and add onchange event
//add an onchange event to the dropdown
getTagFromIdentifierAndTitle("select","DropDownChoice","List name").onchange = function() {ChangeEventListName()};
getTagFromIdentifierAndTitle("select","DropDownChoice","Service").onchange = function() {ChangeEventService()};
getTagFromIdentifierAndTitle("select","DropDownChoice","Source").onchange = function() {ChangeEventSource()};

}

function ChangeEventSource(){
    var source = getTagFromIdentifierAndTitle("select","DropDownChoice","Source");
    var context = new SP.ClientContext.get_current();
	this.website = context.get_web();
    var settingslist = website.get_lists().getByTitle('beyCoo Settings');
    var sourcetext = source.options[source.selectedIndex].text;
    var items = SP.ListOperation.Selection.getSelectedItems();
    var itemId = items[0];
    var currentitem = settingslist.getItemById(itemId);
    currentitem.set_item('Source', sourcetext);
}

//function ChangeEventSource(){
//	var controlsource2 = getTagFromIdentifierAndTitle("select","DropDownChoice","Source");
//	var	hiddentextbox = document.getElementById("ctl00_m_g_3168af66_27c3_40be_bb4f_f2e2f535037d_hiddentextbox");
//	hiddentextbox.value = controlsource2.options[controlsource2.selectedIndex].text;
//	var soapRequest = '<?xml version="1.0" encoding="utf-8"?>' +
//'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">' +
//' <soap12:Body>'+
//' <UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">'+
//' <listName>beyCoo Settings</listName>'+
//' <updates>'+
//'<Batch OnError="Continue">'+
//' <Method ID="1" Cmd="New">'+
//' <Field Name="Source">hiddentextbox.value</Field>'+
//' </Method>'+
//'</Batch>'+
//' </updates>'+
//' </UpdateListItems>'+
//' </soap12:Body>'+
//'</soap12:Envelope>';
//xmlHttp=new XMLHttpRequest();
//xmlHttp.open('post', 'http://kenny/sites/queporttest1/_vti_bin/beyCoo%20Settings.asmx', true);
//xmlHttp.setRequestHeader('Content-Type','application/soap+xml; charset=utf-8');
//xmlHttp.send(soapRequest);
//}

function ChangeEventService(){

    //get the mapping field
    var mapping = getTagFromIdentifierAndTitle("Textarea","TextField","Mapping");

    //alert(mapping);
    //get the service dropdown
    var ddlservice = getTagFromIdentifierAndTitle("select","DropDownChoice","Service");
    //get the selected value
    var selectedService = ddlservice.options[ddlservice.selectedIndex].text;
    if(selectedService.indexOf("Entities") == -1)
    {
        mapping.parentNode.parentNode.parentNode.style.display="none";
    }
    else
    {
        mapping.parentNode.parentNode.parentNode.style.display="";
    }
} 

function ChangeEventListName() {
	var context = new SP.ClientContext.get_current();
	this.website = context.get_web();
	var ddllistname = getTagFromIdentifierAndTitle("select","DropDownChoice","List name");
	var selectedlistname = ddllistname.options[ddllistname.selectedIndex].text;
	this.list = context.get_web().get_lists().getByTitle(selectedlistname);
    context.load(list);
    var controlsource1 = getTagFromIdentifierAndTitle("select","DropDownChoice","Source");
    controlsource1.options.length = 0;
    var fields = list.get_fields();
    //context.load(fields);
    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    getFields(fields, controlsource1);
}

function getFields(fields, controlsource){
   	var fldEnumerator = fields.getEnumerator();
    while(fldEnumerator.moveNext())
    {
      var currentfield = fldEnumerator.get_current();         
      if(!(currentfield.get_hidden()) && !(currentfield.get_readOnlyField())){
           
        var opt = document.createElement("option");
        controlsource.options.add(opt);
        opt.text = currentfield.get_title();
        opt.value = currentfield.get_title();
      }
   	}
}

function onQuerySucceeded(sender, args) {   	
    var textboxlisttype = getTagFromIdentifierAndTitle("input","TextField","List type");
    //get the selected value

    var listtype = '';
	listtype = list.get_baseType();
    var controlsource = getTagFromIdentifierAndTitle("input","TextField","Source");
    if(listtype == '1'){
      textboxlisttype.value = "Document Library";
      controlsource.parentNode.parentNode.parentNode.style.display="none";
    }
    else{
      textboxlisttype.value = "List";
      controlsource.parentNode.parentNode.parentNode.style.display="";          	  
    }
}

function onQueryFailed(sender, args) {
   alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}


//this gets the field based on title identifier and tagname
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
 var len = identifier.length;
 var tags = document.getElementsByTagName(tagName);
 for (var i=0; i < tags.length; i++) {
 var tempString = tags[i].id;
 if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
  return tags[i];
 }
 }
 return null;
}</script>
